package h_10_25;

import java.util.HashMap;

/**
 * 3. 无重复字符的最长子串
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
 */
public class Main1 {
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        char[] ch = s.toCharArray();
        int left = 0;
        int right = 0;
        int n = ch.length;
        int ret = 0;
        while(right < n) {
            // 入
            char x = ch[right];
            map.put(x,map.getOrDefault(x,0) + 1);
            while(map.get(x) > 1) {
                // 出
                char out = ch[left];
                map.put(out,map.get(out) - 1);
                left++;
            }
            // 更新
            ret = Math.max(ret,right - left + 1);
            right++;
        }
        return ret;
    }
}
